/*
 * This file is part of FinVis.
 *
 * FinVis is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * FinVis is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with FinVis.  If not, see <http://www.gnu.org/licenses/>
 *
 * @author Stephen Rudolph
 */
#include "FinInvestmentCommands.h"
#include <iostream>

using namespace std;

/*
 * TODO: Adding and removing investments makes their former ID invalid. Thus,
 * in order to preserve the validity of the undo stack, they should not be
 * removed, but rather 'hidden' or placed back at the old ID
 */

/**
 * Add default investment undo action
 */
AddInvestment::AddInvestment(FinPortfolio *portfolio, QUndoCommand *parent) :
        QUndoCommand(parent)
        , m_portfolio(portfolio)
{
    this->setText(QObject::tr("Add a new investment"));
}

void AddInvestment::undo()
{
    m_portfolio->removeInvestment(m_investmentID);
}

void AddInvestment::redo()
{
    m_portfolio->addAndSelectInvestment();
    m_investmentID = m_portfolio->selectedID();
}

/**
 * Change investment undo action
 */
ChangeInvestment::ChangeInvestment(FinPortfolio *portfolio,
                                   const unsigned int investmentID,
                                   FinInvestment newInvestment,
                                   QUndoCommand *parent) : QUndoCommand(parent)
    , m_oldInvestment(*(portfolio->investmentForID(investmentID)))
    , m_newInvestment(newInvestment)
    , m_investmentID(investmentID)
    , m_portfolio(portfolio)
{
    QString first = "", second = "";
    if (m_newInvestment.principal() != m_oldInvestment.principal())
    {
        first = "$" + QString::number(m_oldInvestment.principal());
        second = "$" + QString::number(m_newInvestment.principal());
    }
    else if (m_newInvestment.investmentOptionVectorIndex() !=
             m_oldInvestment.investmentOptionVectorIndex())
    {
        first =  m_oldInvestment.title();
        second = m_newInvestment.title();
    }

    this->setText(QObject::tr("Change %1 to %2").arg(first, second));
}

void ChangeInvestment::undo()
{
    m_portfolio->setInvestment(m_investmentID,
                             new FinInvestment(m_oldInvestment));
}

void ChangeInvestment::redo()
{
    m_portfolio->setInvestment(m_investmentID,
                             new FinInvestment(m_newInvestment));
}

/**
 * Remove investment undo action
 */
RemoveInvestment::RemoveInvestment(FinPortfolio *portfolio,
                                   const unsigned int investmentID,
                                   QUndoCommand *parent) : QUndoCommand(parent)
    , m_investment(*(portfolio->investmentForID(investmentID)))
    , m_investmentID(investmentID)
    , m_portfolio(portfolio)
{
    this->setText(QObject::tr("Remove %1 %2").arg(m_investment.title(), "$" +
                      QString(m_investment.principal())));
}

void RemoveInvestment::undo()
{
    m_investmentID = m_portfolio->addInvestment(
            new FinInvestment(m_investment));
}

void RemoveInvestment::redo()
{
    m_portfolio->removeInvestment(m_investmentID);
}
